﻿Public Class TMyArguments
    Private _List As New List(Of TMyArgument)
    Private _Owner As IMyRoutine

    Friend Sub New(ByVal AOwner As IMyRoutine)
        _Owner = AOwner
    End Sub
    Private ReadOnly Property Query() As TMyQuery
        Get
            Return _Owner.Query
        End Get
    End Property

    Public Sub Refresh()
        _List.Clear()
        Query.Clear()
        Query.SQL.AppendLine("select")
        Query.SQL.AppendLine(" parameter_name")
        Query.SQL.AppendLine(",data_type")
        Query.SQL.AppendLine(",case when character_maximum_length is null then numeric_precision else character_maximum_length end as arg_length")
        Query.SQL.AppendLine(",numeric_scale")
        Query.SQL.AppendLine(",parameter_mode")
        Query.SQL.AppendLine("from")
        Query.SQL.AppendLine(" information_schema.parameters")
        Query.SQL.AppendLine("where")
        Query.SQL.AppendLine("specific_schema=?schema_name")
        Query.SQL.AppendLine("and specific_name=?procedure_name")
        Query.SQL.AppendLine("and routine_type=?routine_type")
        Query.SQL.AppendLine("order by")
        Query.SQL.AppendLine(" ordinal_position is null")
        Query.SQL.AppendLine(",ordinal_position ")
        Query.DeclareAndSet("schema_name", _Owner.RoutineSchema)
        Query.DeclareAndSet("procedure_name", _Owner.RoutineName)
        Query.DeclareAndSet("routine_type", _Owner.RoutineType)
        Query.Execute()
        While Not Query.Eof
            Dim item As New TMyArgument
            item._ParameterName = Query.FieldAsString("parameter_name")
            item._DataType = Query.FieldAsString("data_type")
            item._ArgLength = Query.FieldAsInteger("arg_length")
            item._NumericScale = Query.FieldAsInteger("numeric_scale")
            item._ParameterMode = Query.FieldAsString("parameter_mode")
            _List.Add(item)
            Query.MoveNext()
        End While
        Query.Close()
    End Sub

    Public ReadOnly Property Count() As Integer
        Get
            Return _List.Count
        End Get
    End Property
    Public ReadOnly Property ReturnArg() As TMyArgument
        Get
            For Each arg As TMyArgument In _List
                If arg.ParameterMode = "RETURNS" Then
                    Return arg
                End If
            Next
            Return Nothing
        End Get
    End Property
    Public ReadOnly Property ItemByName(ByVal AName As String) As TMyArgument
        Get
            For Each arg As TMyArgument In _List
                If arg.ParameterName = AName Then
                    Return arg
                End If
            Next
            Return Nothing
        End Get
    End Property

    Default Public ReadOnly Property Items(ByVal Index As Integer) As TMyArgument
        Get
            Return _List(Index)
        End Get
    End Property
End Class
